

let mCounts = []
let mDelayCounts = []

let i = 0

let t = 0

let NetPrompt = {

    open(delay, id)
    {

        if (!delay && delay !== 0)
        {
            t = 500
        }
        else
        {
            t = delay
        }

        if (t > 0)
        {
            let timer = require('SceneSession').share().addTimer(
                ()=>{

                    let v = null

                    for (let k = 0, len = mDelayCounts.length; k < len; ++k)
                    {
                        v = mDelayCounts[k]
                        if (v.timer === timer)
                        {
                            NetPrompt.open(0, v.id)

                            mDelayCounts.splice(k, 1)

                            break
                        }
                    }
                },
                t
            )
            mDelayCounts.push({ id : ++i, timer : timer })
        }
        else
        {
            let vm = require('ViewMan').share()
            vm.push('NetbusyView')

            mCounts.push(id || ++i)
        }
        
        if (!id)
        {
            Log.d('NetPrompt', 'open id : ' + i)
        }
        return i
    },

    close(id)
    {
        if (id)
        {
            let index = mCounts.indexOf(id)
            if (index > -1)
            {
                mCounts.splice(index, 1)

                if (mCounts.length < 1)
                {
                    let vm = require('ViewMan').share()
                    vm.pop('NetbusyView')
                }
                //Log.d('NetPrompt', 'close id : ' + id)
            }
            else
            {
                let timer = -1

                for (let k = 0, len = mDelayCounts.length; k < len; ++k)
                {
                    if (mDelayCounts[k].id === id)
                    {
                        timer = mDelayCounts[k].timer

                        mDelayCounts.splice(k, 1)

                        break
                    }
                }

                if (timer > 0)
                {
                    require('SceneSession').share().removeTimer(timer)

                    //Log.d('NetPrompt', 'timer close id : ' + id)

                    return
                }
            }
            //Log.w('NetPrompt', 'fail to close id : ' + id)
        }
        else
        {
            mCounts.splice(0, mCounts.length)
            //this.clear()
            
            let vm = require('ViewMan').share()
            vm.pop('NetbusyView')
        }
    },

    clear()
    {
        let len = mCounts.length
        if (len > 0)
        {
            mCounts.splice(0, len)
        }

        len = mDelayCounts.length
        if (len > 0)
        {
            let ss = require('SceneSession').share()

            for (let k = 0; k < len; ++k)
            {
                ss.removeTimer(mDelayCounts[k].timer)
            }

            mDelayCounts.splice(0, len)
        }

        //Log.d('NetPrompt', 'clear all.')
    }
}

module.exports = NetPrompt
